package com.sc.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.context.BaseContext;
import com.sc.domain.Announce;
import com.sc.dto.AnnounceDTO;
import com.sc.dto.AnnouncePageDTO;
import com.sc.mapper.AnnounceMapper;
import com.sc.result.PageResult;
import com.sc.service.AnnounceService;
import org.springframework.stereotype.Service;
import java.time.LocalDate;

@Service
public class AnnounceServiceImpl extends ServiceImpl<AnnounceMapper, Announce> implements AnnounceService {

    /**
     * 添加公告
     *
     * @param announceDTO 前端传入数据
     */
    @Override
    public void saveAnnounce(AnnounceDTO announceDTO) {
        //补全数据
        Announce announce = BeanUtil.copyProperties(announceDTO, Announce.class);
        //发布日期
        announce.setAnDate(LocalDate.now());
        //发布状态
        announce.setAnStatus("已发布");
        //发布人
        Long userId = BaseContext.getCurrentId();
        //根据id查询当前登录用户id todo
        announce.setPublisher("当前登录用户");

        //插入数据
        save(announce);

    }

    /**
     * 修改公告
     *
     * @param announceDTO 前端传入数据
     */
    @Override
    public void updateAnnounce(AnnounceDTO announceDTO) {
        //补全数据
        Announce announce = BeanUtil.copyProperties(announceDTO, Announce.class);
        //发布日期
        announce.setAnDate(LocalDate.now());
        //发布状态
        announce.setAnStatus("已发布");
        //发布人
        Long userId = BaseContext.getCurrentId();
        //根据id查询当前登录用户id todo
        announce.setPublisher("当前登录用户");

        //更新数据
        updateById(announce);
    }

    /**
     * 分页查询活动
     *
     * @param announcePageDTO 前端传入数据
     * @return 分页结果
     */
    @Override
    public PageResult<Announce> pageQuery(AnnouncePageDTO announcePageDTO) {
        Page<Announce> pageInfo = new Page<>(announcePageDTO.getPage(),announcePageDTO.getPageSize());
        LambdaQueryWrapper<Announce> queryWrapper = null;
        if(announcePageDTO.getTitle()!=null) {
            queryWrapper =  new LambdaQueryWrapper<Announce>().like(Announce::getTitle,announcePageDTO.getTitle());
        }
        Page<Announce> page = page(pageInfo, queryWrapper);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }
}
